package com.advertisementsystem.Servie;

import com.advertisementsystem.vo.GeoResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class AmapService {
    private static final String GEOCODE_URL = "https://restapi.amap.com/v3/geocode/geo";

    @Value("${amap.key}")
    private String apiKey;

    @Resource
    private RestTemplate restTemplate;

    public GeoResult getGeoCode(String address) {
        Map<String, String> params = new HashMap<>();
        params.put("key", apiKey);
        params.put("address", address);

        ResponseEntity<String> response = restTemplate.getForEntity(
                GEOCODE_URL + "?key={key}&address={address}",
                String.class,
                params
        );

        return parseResponse(response.getBody());
    }

    private GeoResult parseResponse(String json) {
        JSONObject result = JSON.parseObject(json);
        if ("1".equals(result.getString("status"))) {
            JSONArray geocodes = result.getJSONArray("geocodes");
            if (geocodes != null && !geocodes.isEmpty()) {
                JSONObject geo = geocodes.getJSONObject(0);
                String[] location = geo.getString("location").split(",");
                return new GeoResult(
                        new BigDecimal(location[1]),
                        new BigDecimal(location[0])
                );
            }
        }
        return null;
    }


    /**
     * 根据经纬度获取地址信息
     * @param longitude 经度
     * @param latitude 纬度
     * @return 地址字符串
     */
    public String getAddressByLocation(BigDecimal longitude, BigDecimal latitude) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("key", apiKey);
            params.put("location", longitude + "," + latitude);

            String url = "https://restapi.amap.com/v3/geocode/regeo";
            ResponseEntity<String> response = restTemplate.getForEntity(
                    url + "?key={key}&location={location}",
                    String.class,
                    params
            );

            if (response.getStatusCode().is2xxSuccessful()) {
                JSONObject result = JSON.parseObject(response.getBody());
                if ("1".equals(result.getString("status"))) {
                    JSONObject regeocode = result.getJSONObject("regeocode");
                    if (regeocode != null) {
                        return regeocode.getString("formatted_address");
                    }
                }
            }
        } catch (Exception e) {
            log.error("根据经纬度获取地址失败", e);
        }
        return null;
    }
}